var $=jQuery.noConflict();
jQuery(document).ready(function(){
	
	var interval_function = setInterval('rotate()', 10000);
	
	captify();
	jQuery('ul.blog li').addClass('hide');
	jQuery('ul.blog li:first').removeClass('hide').addClass('show');
	
	jQuery('#rotator_prev').bind('click', function(){ rotate_prev(); });
	jQuery('#rotator_play').bind('click', function(){
	
		var me = jQuery(this);
		if(me.data("status") == null)
			me.data("status", "play");
		
		if(me.data("status") == "play"){
			me.data("status", "stop");
			jQuery(this).text("stop");
			rotator(true, interval_function);
		}
		else{
			me.data("status", "play");
			jQuery(this).text("play");
			rotator(false, interval_function);
		}
	});
	jQuery('#rotator_next').bind('click', function(){ rotate(); });
	
	jQuery('#rotator_play').click();
});

function captify(){
	jQuery(" ul.blog li").each(
		
		function(i, element){
			var header  = jQuery(" .contentheading", element);
			header.remove();
		    var title   = header.text();
			jQuery(" .contentpaneopen", element).each(
				
				function(j, pane){
					var ps=jQuery(" p", pane);
					var a =jQuery(" a.readon", pane);
					a.addClass("read_more");
					if(ps.size() >= 2){
						
						jQuery(" tbody tr td :first", pane).append("<div class='temp_div'></div>");
						
						var image = jQuery(" img", ps);
						image.addClass("captify");
						var alt    = "<p class='title''>"+title+"</p>"+ps.text();
						var a_html = jQuery(".temp_div", pane).append(a.clone()).remove().html();
						
						if(a_html)
							alt += a_html;
						image.attr("alt", alt);
						image.detach();

						ps.parent().append(image);
						ps.remove();
						a.parent().remove();
					}
				}
				
			);
		}
		
	);
	
	jQuery("img.captify").captify({
		spanWidth: '95%'
	});

}

function rotator(signal, interval_function){
	
	if(signal)
		interval_function;
	else
		clearInterval(interval_function);
	
}

function rotate() {	
	
	//Get the first image
	var current = (jQuery('ul.blog li.show:first') ?  jQuery('ul.blog li.show:first') : jQuery('ul.blog li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
	var next = current.next();
	if(next.length == 0)
		next = jQuery('ul.blog li:first');
	
	next.addClass('show').removeClass('hide');
	//.animate({display: 'block'}, 1000);

	//Hide the current image
	//current.animate({display: 'none'}, 1000)
	current.removeClass('show').addClass('hide');
	
};

function rotate_prev() {	
	
	//Get the first image
	var current = (jQuery('ul.blog li.show:first') ?  jQuery('ul.blog li.show:first') : jQuery('ul.blog li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
	var prev = current.prev();
	if(prev.length == 0)
		prev = jQuery('ul.blog li:last');
	
	
	prev.addClass('show').removeClass('hide');
	//.animate({display: 'block'}, 1000);

	//Hide the current image
	//current.animate({display: 'none'}, 1000)
	current.removeClass('show').addClass('hide');
	
};
